#include <REGX52.H>
#include "Delay.h"

/**
  * @brief   获取矩阵键盘按下哪个键
  * @details 
  * @retval  1-16对应S1-S16，0表示没有按下
  */
unsigned char MatrixKey() {

	unsigned char pressKey = 0;
	
	// 判断是否按下第一列
	P1 = 0xF7;
	if (P1 != 0xF7) {
		Delay(10);
		switch (P1) {
			case 0x77: pressKey = 1; break;
			case 0xB7: pressKey = 5; break;
			case 0xD7: pressKey = 9; break;
			case 0xE7: pressKey = 13; break;
		}
		while (P1 != 0xF7);
		Delay(10);
	}
	
	// 判断是否按下第二列
	P1 = 0xFB;
	if (P1 != 0xFB) {
		Delay(10);
		switch (P1) {
			case 0x7B: pressKey = 2; break;
			case 0xBB: pressKey = 6; break;
			case 0xDB: pressKey = 10; break;
			case 0xEB: pressKey = 14; break;
		}
		while (P1 != 0xFB);
		Delay(10);
	}
	
	// 判断是否按下第三列
	P1 = 0xFD;
	if (P1 != 0xFD) {
		Delay(10);
		switch (P1) {
			case 0x7D: pressKey = 3; break;
			case 0xBD: pressKey = 7; break;
			case 0xDD: pressKey = 11; break;
			case 0xED: pressKey = 15; break;
		}
		while (P1 != 0xFD);
		Delay(10);
	}
	
		// 判断是否按下第四列
	P1 = 0xFE;
	if (P1 != 0xFE) {
		Delay(10);
		switch (P1) {
			case 0x7E: pressKey = 4; break;
			case 0xBE: pressKey = 8; break;
			case 0xDE: pressKey = 12; break;
			case 0xEE: pressKey = 16; break;
		}
		while (P1 != 0xFE);
		Delay(10);
	}
	
	return pressKey;
}